package com.chuhe.starry.dao;

import com.chuhe.common.domain.entity.GroupInfo;
import com.chuhe.common.domain.enums.GroupStatusEnum;
import com.chuhe.common.domain.vo.req.AdminLoadGroupReq;
import com.chuhe.common.domain.vo.resp.LoadGroupResp;
import com.chuhe.starry.mapper.GroupInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张楚河
 * @since 2025-05-12
 */
@Service
public class GroupInfoDao extends ServiceImpl<GroupInfoMapper, GroupInfo> {
    /**
     * 查询已设置群组数量
     * @Author: 楚河
     * @Date: 2025-05-13 23:05
     */
    public Integer selectCount(String groupOwnerId) {
        return lambdaQuery()
                .eq(GroupInfo::getGroupOwnerId, groupOwnerId)
                .count();
    }
    /**
     * 根据群组id查询
     * @Author: 楚河
     * @Date: 2025-05-14 15:12
     */
    public GroupInfo selectByGroupId(String groupId) {
        return lambdaQuery()
                .eq(GroupInfo::getGroupId, groupId)
                .one();
    }
    /**
     * 修改群组
     * @Author: 楚河
     * @Date: 2025-05-14 16:10
     */
    public void updateGroup(GroupInfo groupInfo) {
        lambdaUpdate()
                .eq(GroupInfo::getGroupId, groupInfo.getGroupId())
                .set(GroupInfo::getGroupName,groupInfo.getGroupName())
                .set(GroupInfo::getGroupNotice,groupInfo.getGroupNotice())
                .set(GroupInfo::getJoinType,groupInfo.getJoinType())
                .set(GroupInfo::getStatus,groupInfo.getStatus())
                .update();
    }
    /**
     * 获取群组，时间倒序
     * @Author: 楚河
     * @Date: 2025-05-14 16:10
     */
    public List<LoadGroupResp> selectGroupList(String userId) {
        return lambdaQuery()
                .eq(GroupInfo::getGroupOwnerId, userId)
                .eq(GroupInfo::getStatus,GroupStatusEnum.NORMAL.getStatus())
                .orderByDesc(GroupInfo::getCreateTime)
                .list()
                .stream()
                .map(a -> {
                    LoadGroupResp resp = new LoadGroupResp();
                    BeanUtils.copyProperties(a, resp);
                    return resp;
                })
                .collect(Collectors.toList());
    }

    /**
     * 查询群聊详情
     * @Author: 楚河
     * @Date: 2025-05-14 19:40
     */
    public GroupInfo getGroupByGroupid(String groupId) {
        return lambdaQuery()
                .eq(GroupInfo::getGroupId, groupId)
                .one();
    }
    /**
     * 根据id查询群组
     * @Author: 楚河
     * @Date: 2025-05-15 22:53
     */
    public GroupInfo selectGroupId(String contactId) {
        return lambdaQuery()
                .eq(GroupInfo::getGroupId, contactId)
                .one();
    }
    /**
     * 查询数量
     * @Author: 楚河
     * @Date: 2025-05-18 14:15
     */
    public int selectGroupCount() {
        return lambdaQuery()
                .count();
    }
    /**
     * 查询群组 带条件
     * @Author: 楚河
     * @Date: 2025-05-18 14:17
     */
    public List<GroupInfo> selectAllGroup(AdminLoadGroupReq req) {
        return lambdaQuery()
                .eq(StringUtils.isNotBlank(req.getGroupId()),GroupInfo::getGroupId, req.getGroupId())
                .like(StringUtils.isNotBlank(req.getGroupNameFuzzy()),GroupInfo::getGroupName, req.getGroupNameFuzzy())
                .eq(StringUtils.isNotBlank(req.getGroupOwnerIdFuzzy()),GroupInfo::getGroupOwnerId, req.getGroupOwnerIdFuzzy())
                .list();
    }
    /**
     * 删除群组
     * @Author: 楚河
     * @Date: 2025-05-18 14:33
     */
    public void delGroupByGroupId(String groupId) {
        lambdaUpdate()
                .eq(GroupInfo::getGroupId, groupId)
                .set(GroupInfo::getStatus, GroupStatusEnum.DISSOLUTION.getStatus())
                .update();
    }
}
